﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using CMS.BLL;
using CMS.BLL.Entity;
using Component.Worker.RecursiveProcess;

public partial class AdminX_Testimonial_TestimonialAdd : System.Web.UI.UserControl
{
    private int nDefaultLang = Support.GetDefaultLanguage();
    int nCurrentLang = 1;
    protected void Page_Load(object sender, EventArgs e)
    {
        
        if (!IsPostBack)
        {
            this.SetLabelLanguage();
            this.ConfigCkeditor();
            SetValidate();
            InitData();
        }
    }

    private void SetLabelLanguage()
    {
        int nDefaultLanguage = nDefaultLang;
        nDefaultLanguage = nDefaultLanguage - 1;
        lblContent.Text = MultipleLanguage.LBL_CONTENT[nDefaultLanguage];
        lblImage.Text = MultipleLanguage.LBL_IMAGE[nDefaultLanguage];
        lblFullname.Text = MultipleLanguage.LBL_FULLNAME[nDefaultLanguage];
        lblClientPosition.Text = MultipleLanguage.LBL_CLIENT_POSITION[nDefaultLanguage];
        lbleActive.Text = MultipleLanguage.LBL_ACTIVE[nDefaultLanguage];
        btnSave.Text = MultipleLanguage.BTN_SAVE[nDefaultLanguage];
        btnCancel.Text = MultipleLanguage.BTN_BACK[nDefaultLanguage];


    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        string sGoToPageMan = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_TESTIMONIAL;
        Response.Redirect(sGoToPageMan);
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        string sContent = ConvertData.ConvertToString(txtcontent.Text);
        if (sContent != "")
        {

            int nDefaultLanguage = nDefaultLang;
            nDefaultLanguage = nDefaultLanguage - 1;
            int nActionResult = 0;
            int nCurrentLanguage = 1;
            Testimonial objTestimonial = new Testimonial();
            objTestimonial.Data = SetData(nCurrentLanguage);
            string sAction = GetQuerryString(Config.ACTION);
            if (sAction == Config.ACTION_EDIT)
            {
                //Is Edit
                int nItemID = ConvertData.ConvertToInt(Request.QueryString[Config.ACTION_ID]);
                nActionResult = objTestimonial.Update(nItemID);
                if (nActionResult > 0)
                {

                    // Show updated image
                    if (objTestimonial.Data.Image.ToString() != "")
                    {
                        imgImage.ImageUrl = Config.ROOT + Config.IMAGE_TESTIMONIAL_DEFAULT_UPLOAD + objTestimonial.Data.Image.ToString();
                        lbimg.Text = ConvertData.ConvertToString(objTestimonial.Data.Image);
                        urlImg.NavigateUrl = Config.ROOT + Config.IMAGE_TESTIMONIAL_DEFAULT_UPLOAD + objTestimonial.Data.Image;
                    }

                    MessageBoxss.Show(MessagesAlert.UPDATE_SUCCESSFUL[nDefaultLang]);

                }
                else

                    MessageBoxss.Show(MessagesAlert.UPDATE_UNSUCCESSFUL[nDefaultLang]);
            }
            else
            {
                // Is Insert New

                nActionResult = objTestimonial.Insert();
                if (nActionResult > 0)
                {
                    string sURL = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_TESTIMONIAL + "&" + Config.MESS_ID + "=" + Config.MESSAGE_INSERT;
                    Response.Redirect(sURL);
                }


            }
        }
        else
        {
            txtcontent.Focus();
            MessageBoxss.Show(MessagesValidate.NEWS_CONTENT_EMPTY[nDefaultLang - 1]);
        }
    }

    #region Relative Functions

    protected void ConfigCkeditor()
    {

        txtcontent.Language = MultipleLanguage.CKEDITOR_LANGUAGE[nDefaultLang];
        txtcontent.config.filebrowserImageBrowseUrl = Config.FILEBROWSERIMAGE_BROWSE;
        txtcontent.config.filebrowserImageUploadUrl = Config.FILEBROWSERIMAGE_UPLOAD;
        CKFinder.FileBrowser _FileBrowser = new CKFinder.FileBrowser();
        _FileBrowser.BasePath = Config.FILEBROWSER;
        _FileBrowser.SetupCKEditor(txtcontent);
    }

    private void SetValidate()
    {
        rfvTitle.ErrorMessage = MessagesValidate.NEWS_TESTIMONIAL_EMPTY[nDefaultLang - 1];
       

    }

    private void InitData()
    {
        
        string sQueery = ConvertData.ConvertToString(Request.QueryString[Config.ACTION]);
        int nSessionLangID = ConvertData.ConvertToInt(Session[Config.SESSION_LANGID]);
        try
        {
            if (sQueery == Config.ACTION_EDIT)
            {
                int nItemID = ConvertData.ConvertToInt(Request.QueryString[Config.ACTION_ID]);

                if (nItemID > 0)
                    GetData(nCurrentLang, nItemID);

                Session[Config.SESSION_LANGID] = "1";
            }
            else { imgImage.ImageUrl = Config.IMAGE_NO_IMAGE; }

        }
        catch
        {
            string sRedirect = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_TESTIMONIAL + "&" + Config.MESS_ID + "=" + Config.ITEM_NOTEXIST;
            Response.Redirect(sRedirect);
        }
    }

    private void GetData(int CurrentLang, int ItemID)
    {
        // Get data to view on UI Controls
        int nStatus = 0;


        Testimonial objTestimonial = new Testimonial();
        objTestimonial.LoadById(ItemID);

        nStatus = ConvertData.ConvertToInt(objTestimonial.Data.Status);
        if (nStatus > 0)
            chkActive.Checked = true;
        else chkActive.Checked = false;


        txtFullname.Text = objTestimonial.Data.ClientName;
        txtClientPosition.Text = objTestimonial.Data.ClientPosition;
                txtcontent.Text = (ConvertData.ConvertToString(objTestimonial.Data.TestimonialContent));
               
       
        if (objTestimonial.Data.Image!= "")
            imgImage.ImageUrl = Config.ROOT + Config.IMAGE_TESTIMONIAL_DEFAULT_UPLOAD + objTestimonial.Data.Image;
        else imgImage.ImageUrl = Config.IMAGE_NO_IMAGE;
        lbimg.Text = ConvertData.ConvertToString(objTestimonial.Data.Image);
        urlImg.NavigateUrl = Config.ROOT + Config.IMAGE_TESTIMONIAL_DEFAULT_UPLOAD + objTestimonial.Data.Image;
     
    }

    private eTestimonial TestimonialEntity()
    {
        eTestimonial entityNews = new eTestimonial();
        int nItemID = ConvertData.ConvertToInt(Request.QueryString[Config.ACTION_ID]);
        string sQueery = ConvertData.ConvertToString(Request.QueryString[Config.ACTION]);
        if (nItemID > 0 && sQueery == Config.ACTION_EDIT)
        {
            Testimonial objTestimonial = new Testimonial();
            objTestimonial.LoadById(nItemID);
            entityNews = objTestimonial.Data;
        }
        return entityNews;
    }

    private eTestimonial SetData(int CurrentLang)
    {
        string sQueery = ConvertData.ConvertToString(Request.QueryString[Config.ACTION]);
        eTestimonial eTestimonialEntity = this.TestimonialEntity();
        Testimonial objTestimonial = new Testimonial();
        if (chkActive.Checked) objTestimonial.Data.Status = Config.STATUS_ACTIVE;
        else objTestimonial.Data.Status = Config.STATUS_INACTIVE;

        objTestimonial.Data.ClientName = ConvertData.ConvertToString(txtFullname.Text);
        objTestimonial.Data.ClientPosition = ConvertData.ConvertToString(txtClientPosition.Text);
        objTestimonial.Data.TestimonialContent = ConvertData.ConvertToString(txtcontent.Text);
        if (iptImage.Value != "")
        {
            Support.DeleteFile("testimonial", eTestimonialEntity.Image);
            objTestimonial.Data.Image = Support.UploadFile(iptImage, "testimonial");
        }
        else
        {
            if (eTestimonialEntity.Image != "")
                objTestimonial.Data.Image = ConvertData.ConvertToString(eTestimonialEntity.Image);
            else objTestimonial.Data.Image = "";
            objTestimonial.Data.Image = ConvertData.ConvertToString(eTestimonialEntity.Image);
        }

  
        return objTestimonial.Data;
    }

    private string GetQuerryString(string sQuerry)
    {
        string sQuerryString = "";
        sQuerryString = ConvertData.ConvertToString(Request.QueryString[sQuerry]);
        return sQuerryString;
    }
    #endregion

}